/**
 * LinkedLST
 * May 20, 2011
 * ConversionTestCase.java - Tests for Conversion.java
 */

package edu.washington.cs.labspecimentransport.test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.conn.ConnectTimeoutException;

import edu.washington.cs.labspecimentransport.dataobjects.Item;
import edu.washington.cs.labspecimentransport.ops.Conversion;
import junit.framework.TestCase;

public class ConversionTestCase extends TestCase {
	
	Item i1;
	Item i2;
	Item i3;
	Item i4;
	Item i5;
	
	List<Item> empty;
	List<Item> single;
	List<Item> many;
	
	List<Integer> emptyInts;
	List<Integer> populatedInts;
	
	int id1;
	int id2;
	
	String expected1;
	String expected2;
	
	public ConversionTestCase() {
		i1 = new Item();
		i2 = new Item();
		i3 = new Item();
		i4 = new Item();
		i5 = new Item();
		
		i1.set_id(1);
		i2.set_id(2);
		i3.set_id(3);
		i4.set_id(4);
		i5.set_id(5);
		
		empty = new ArrayList<Item>();
		
		single = new ArrayList<Item>();
		single.add(i1);
		
		many = new ArrayList<Item>();
		many.add(i1);
		many.add(i2);
		many.add(i3);
		many.add(i4);
		many.add(i5);
		
		emptyInts = new ArrayList<Integer>();
		populatedInts = new ArrayList<Integer>();
		
		id1 = 1060745339;
		id2 = 1060745347;
		populatedInts.add(id1);
		populatedInts.add(id2);
		
		expected1 = "16bncvPiO";
		expected2 = "blah";
	}
	
	// ======================
	// itemsToItemIds
	// ======================
	
	// empty list to convert
	public void testItemsToItemIdsEmpty() {
		ArrayList<Integer> result = Conversion.convertItemsToItemIds(empty);
		assertEquals(result.size(), 0);
		assertTrue(result.isEmpty());
	}
	
	// list with a single item to convert
	public void testItemsToItemIdsSingle() {
		ArrayList<Integer> result = Conversion.convertItemsToItemIds(single);
		assertEquals(result.size(), 1);
		assertTrue(result.get(0) == 1);
	}
	
	// list with many items to convert
	public void testItemsToItemIdsMany() {
		ArrayList<Integer> result = Conversion.convertItemsToItemIds(many);
		assertEquals(result.size(), 5);
		
		assertTrue(result.get(0) == 1);
		assertTrue(result.get(1) == 2);
		assertTrue(result.get(2) == 3);
		assertTrue(result.get(3) == 4);
		assertTrue(result.get(4) == 5);
	}
	
	// ======================
	// itemIdListToBarcodeList
	// ======================
	
	// list with no items to convert
	public void testItemIdsToBarcodesEmpty() {
		try {
			Map<Integer, ArrayList<String>> result = 
				Conversion.convertItemIdListToBarcodeList(emptyInts);
			assertTrue(result.isEmpty());
		} catch (ConnectTimeoutException e) {
			fail("Unable to connect to server");
		}
	}

}
